﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace MainApp
{
    public partial class License : DevExpress.XtraEditors.XtraForm
    {
        public License()
        {
            InitializeComponent();
        }
        public bool isValidDateTime(String current,String latest)
        {
            int yearC = Convert.ToInt32(current.Substring(0,4));
            int monthC=Convert.ToInt32(current.Substring(4,2));
            int dayC=Convert.ToInt32(current.Substring(6,2));
            DateTime curDate = new DateTime(yearC,monthC,dayC);
            int yearL = Convert.ToInt32(latest.Substring(0,4));
            int monthL =Convert.ToInt32(latest.Substring(4,2));
            int dayL =Convert.ToInt32(latest.Substring(6,2));
        DateTime latDate = new DateTime(yearL,monthL,dayL);
            if(curDate>=latDate)
            {
                return true;
            }
            return false;
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                String strFileName1 = "System.administrator.decode.dll";

                FileStream fileStream1 = new FileStream(strFileName1, FileMode.Open);

                String currentDateTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                byte[] arr = new byte[8];
                fileStream1.Read(arr, 0, 8);

                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                String LatestDatetime = encoding.GetString(arr);
                if (isValidDateTime(currentDateTime, LatestDatetime) && isValidDateTime("20111019", currentDateTime))
                {
                    fileStream1.Write(encoding.GetBytes(currentDateTime), 0, currentDateTime.Length);

                }
                else
                {
                    MessageBox.Show("Ma khong hop le");
                    return;
                }
                if (!textBox1.Text.Equals("Au^9Ke)pQIE"))
                {
                    MessageBox.Show("Ma khong hop le");
                    return;
                }
                
                FrmMain formMain = new FrmMain();
                formMain.StartPosition = FormStartPosition.CenterScreen;
                Visible = false;
                formMain.Show();
                //  System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
                //  fileStream1.Write(encoding.GetBytes(expiredDate), 0, expiredDate.Length);
                //  fileStream2.Write(encoding.GetBytes(expiredDate), 0, expiredDate.Length);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Mã chạy không hợp lệ! ex");
            }
           
        }
    }
}